package com.example.sherlockzhong.plush_v2;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.Toast;

import com.example.sherlockzhong.plush_v2.CardBeans.ConsumptionCardBean;
import com.example.sherlockzhong.plush_v2.CardBeans.LessonCardBean;
import com.example.sherlockzhong.plush_v2.CardBeans.NewsCardBean;
import com.example.sherlockzhong.plush_v2.CardBeans.TodoCardBean;
import com.example.sherlockzhong.plush_v2.CardBeans.WeatherCardBean;
import com.example.sherlockzhong.plush_v2.CardIni.ConsumptionCardIni;
import com.example.sherlockzhong.plush_v2.CardIni.LessonCardIni;
import com.example.sherlockzhong.plush_v2.CardIni.NewsCardIni;
import com.example.sherlockzhong.plush_v2.CardIni.TodoCardIni;
import com.example.sherlockzhong.plush_v2.CardIni.WeatherCardIni;
import com.example.sherlockzhong.plush_v2.Models.ClassesModel;
import com.example.sherlockzhong.plush_v2.Models.DataModel;
import com.example.sherlockzhong.plush_v2.Subscriptions.SubscriptionActivity;
import com.example.sherlockzhong.plush_v2.Utils.AnimationUtil;
import com.example.sherlockzhong.plush_v2.Utils.DateTimeUtil;
import com.example.sherlockzhong.plush_v2.Utils.DialogUtil;
import com.example.sherlockzhong.plush_v2.Utils.StorageUtils;
import com.example.sherlockzhong.plush_v2.View.MainContentFrame;
import com.umeng.fb.FeedbackAgent;
import com.umeng.update.UmengUpdateAgent;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by xujiachong on 14-9-19.
 */

public class MainController {

    public static float SCALE;
    public boolean historyDataLoaded = false;
    public JSONArray allData = new JSONArray();
    public JSONArray refreshedData;
    public Boolean isDetail = false;
    public int width;
    Runnable reflashRunnable = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            MainController.this.reflashJSON();
            MainController.this.mainActivity.mHandler.obtainMessage(1).sendToTarget();
        }
    };
    private Boolean isNotificated = false;
    private Boolean isReflashing = false;
    Handler handler = new Handler() {
        /**
         * 0x200 同步数据成功
         * 0x401 同步数据失败
         * */
        @Override
        public void handleMessage(Message msg) {
            mainActivity.stopReflashDialog();
            if (msg.what == 0x200) {
                Toast.makeText(mainActivity, "同步数据成功", Toast.LENGTH_LONG).show();
            } else if (msg.what == 0x401) {
                Toast.makeText(mainActivity, "网络超时", Toast.LENGTH_SHORT).show();
            }//动画breath开始
            else if (msg.what == 0x123) {
                if (isReflashing)
                    startBreath();
                //Log.e("isReflash = ", isReflashing.toString());
            }
            //动画breath_reverse回复
            else if (msg.what == 0x321) {
                //if(isReflashing)
                startBreathReverse();
            }
        }
    };
    private boolean titlePressed = false;
    private Intent sIntent;
    private Thread mThread = null;
    private MainActivity mainActivity;
    private int newCardCount = 0;
    //呼吸的plush
    private View button;
    private Animation breath;
    private Animation breath_reverse;

    public MainController(MainActivity activity) {
        this.mainActivity = activity;
//        JSONArray history = DataModel.retrieveHistoryData();
//        if (history != null) {
//            this.allData = history;
//        }
    }

    /**
     * dp转像素
     */
    public static int DP_TO_PX(float dpValue) {
        return (int) (dpValue * SCALE + 0.5f);
    }

    private void startBreath() {
        breath = AnimationUtils.loadAnimation(mainActivity, R.anim.breath);
        breath.setFillAfter(true);
        button.startAnimation(breath);
        Integer status = button.getVisibility();
        Log.e("button ="+button+"refresh = " + isReflashing.toString(), "status = " + status.toString());
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0x321);
            }
        }, 1000);
    }

    private void startBreathReverse() {
        breath_reverse = AnimationUtils.loadAnimation(mainActivity, R.anim.breath_reserve);
        breath_reverse.setFillAfter(true);
        button.startAnimation(breath_reverse);
        Integer status = button.getVisibility();
        Log.e("button ="+button+"refresh = " + isReflashing.toString(), "status = " + status.toString());
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0x123);
            }
        }, 1000);
    }

    public void pressTitle(View v) {
        if (!titlePressed) {
            this.titlePressed = true;
            if (this.isDetail) {
                this.titlePressed = false;
                this.showCards();
                return;
            }
            if (!this.isReflashing) {
                mainActivity.ll_cover.setVisibility(View.VISIBLE);
                this.reflash(v);
            }

        }
    }

    public void pressSetting(View v) {
        if (this.isDetail) {
            this.showCards();
            return;
        }
        this.mainActivity.ll_main.setVisibility(View.GONE);
        this.mainActivity.ll_setting.setVisibility(View.VISIBLE);
    }

    public void pressSubscribe(View v) {
        if (this.isDetail) {
            this.showCards();
            return;
        }
        final View v_final = v;
        AnimationSet as = new AnimationSet(true);
        final float w = v_final.getWidth() / 2;
        final float h = v_final.getHeight() / 2;
        RotateAnimation ra = new RotateAnimation(0, 180, w, h);
        ra.setDuration(300);
        as.addAnimation(ra);
        v_final.startAnimation(as);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                jumpToSubscribe();
            }
        }, 250);
    }

    public void pressCollection(View v) {
        if (this.isDetail) {
            this.showCards();
            return;
        }
        AnimationSet as = new AnimationSet(true);
        final float w = v.getWidth() / 2;
        final float h = v.getHeight() / 2;
        RotateAnimation ra = new RotateAnimation(0, 360, w, h);
        ra.setDuration(200);
        as.addAnimation(ra);
        v.startAnimation(as);
        as.setFillAfter(true);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Intent intent = new Intent(mainActivity, CollectionActivity.class);
                mainActivity.startActivityForResult(intent, 0);
            }
        }, 200);
    }

    public void pressSettingDone(View v) {
        this.mainActivity.ll_main.setVisibility(View.VISIBLE);
        this.mainActivity.ll_setting.setVisibility(View.GONE);
    }

    /**
     * 停止请求方法
     */
    public void stopRequest() {
        this.mainActivity.mHandler.removeCallbacksAndMessages(null);
        if (this.mThread != null) {
            if (mThread.isAlive()) {
                mThread.interrupt();
            }
        }
    }

    public void loadHistoryData() {
        this.mainActivity.main_content_frame.removeAllViews();
        this.mainActivity.sv_cards.scrollTo(0, 0);
        try {
            this.allData = new JSONArray(StorageUtils.retrieveFromFile("HistoryData.json"));
            for (int i = 0; i < this.allData.length(); i++) {
                JSONObject cardData = (JSONObject) this.allData.get(i);
                generateCard(cardData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        cardsFlyIn(mainActivity.main_content_frame);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                titlePressed = false;
                mainActivity.ll_cover.setVisibility(View.GONE);
                System.out.println("gone");
            }
        }, 1250);
    }

    private void reflash() {
        this.isReflashing = true;
        this.mThread = new Thread(reflashRunnable);
        this.mThread.start();
        NotificationManager nm = (NotificationManager) this.mainActivity.getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(100);
    }


    private void reflash(View v) {
        this.isReflashing = true;
        //动画开始
        button = v;
        handler.sendEmptyMessage(0x123);
        this.mThread = new Thread(reflashRunnable);
        this.mThread.start();
        NotificationManager nm = (NotificationManager) this.mainActivity.getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(100);
    }

    public void showDetail() {
        this.isDetail = true;
        this.mainActivity.sv_cards.setVisibility(View.GONE);
        this.mainActivity.ll_detail.setVisibility(View.VISIBLE);
        AnimationSet as = new AnimationSet(true);
        AlphaAnimation aa = new AlphaAnimation(0, 1);
        aa.setDuration(500);
        as.addAnimation(aa);
        this.mainActivity.ll_detail.startAnimation(as);
    }

    public void showCardInner(int index) {
        try {
            JSONObject cardData = allData.getJSONObject(index);
            String cardType = cardData.getString("card_type");
            if (cardType.equalsIgnoreCase("weather")) {
                showWeatherDetail(
                        Integer.parseInt(cardData.getString("TodayIcon")),
                        cardData.getString("Address"),
                        cardData.getString("Condition"),
                        cardData.getString("WindPower"),
                        cardData.getString("Temperature"),
                        cardData.getString("Dress"),
                        cardData.getString("Cold"),
                        cardData.getString("Sport"),
                        cardData.getString("Uva"),
                        cardData.getString("DressIndex"),
                        cardData.getString("ColdIndex"),
                        cardData.getString("SportIndex"),
                        cardData.getString("UvaIndex"));
            } else if (cardType.equalsIgnoreCase("organization") || cardType.equalsIgnoreCase("myclass") || cardType.equalsIgnoreCase("tutor") || cardType.equalsIgnoreCase("official")) {
                showThreeDetail(cardData.getString("title"), cardData.getString("create_time"), cardData.getString("content"));
            } else if (cardType.equalsIgnoreCase("class")) {
                JSONArray classesOfDay = ClassesModel.findByDay(DateTimeUtil.getWeekdayIndex());

                JSONObject classObject1 = classesOfDay.getJSONObject(0);
                JSONObject classObject2 = classesOfDay.getJSONObject(1);
                JSONObject classObject3 = classesOfDay.getJSONObject(2);
                JSONObject classObject4 = classesOfDay.getJSONObject(3);
                JSONObject classObject5 = classesOfDay.getJSONObject(4);
                JSONObject classObject6 = classesOfDay.getJSONObject(5);

                String class1 = (classObject1.getString("class_name").equalsIgnoreCase("empty")) ? "" : classObject1.getString("class_name");
                String class2 = (classObject2.getString("class_name").equalsIgnoreCase("empty")) ? "" : classObject2.getString("class_name");
                String class3 = (classObject3.getString("class_name").equalsIgnoreCase("empty")) ? "" : classObject3.getString("class_name");
                String class4 = (classObject4.getString("class_name").equalsIgnoreCase("empty")) ? "" : classObject4.getString("class_name");
                String class5 = (classObject5.getString("class_name").equalsIgnoreCase("empty")) ? "" : classObject5.getString("class_name");
                String class6 = (classObject6.getString("class_name").equalsIgnoreCase("empty")) ? "" : classObject6.getString("class_name");

                String classTime1 = (classObject1.has("class_order")) ? classObject1.getString("class_order") + " (" + classObject1.getString("weeks_sumary") + ")" : "";
                String classTime2 = (classObject2.has("class_order")) ? classObject2.getString("class_order") + " (" + classObject2.getString("weeks_sumary") + ")" : "";
                String classTime3 = (classObject3.has("class_order")) ? classObject3.getString("class_order") + " (" + classObject3.getString("weeks_sumary") + ")" : "";
                String classTime4 = (classObject4.has("class_order")) ? classObject4.getString("class_order") + " (" + classObject4.getString("weeks_sumary") + ")" : "";
                String classTime5 = (classObject5.has("class_order")) ? classObject5.getString("class_order") + " (" + classObject5.getString("weeks_sumary") + ")" : "";
                String classTime6 = (classObject6.has("class_order")) ? classObject6.getString("class_order") + " (" + classObject6.getString("weeks_sumary") + ")" : "";

                String classroom1 = (classObject1.has("class_room")) ? classObject1.getString("class_room") : "";
                String classroom2 = (classObject2.has("class_room")) ? classObject2.getString("class_room") : "";
                String classroom3 = (classObject3.has("class_room")) ? classObject3.getString("class_room") : "";
                String classroom4 = (classObject4.has("class_room")) ? classObject4.getString("class_room") : "";
                String classroom5 = (classObject5.has("class_room")) ? classObject5.getString("class_room") : "";
                String classroom6 = (classObject6.has("class_room")) ? classObject6.getString("class_room") : "";

                String teacher1 = (classObject1.has("teacher_name")) ? classObject1.getString("teacher_name") : "";
                String teacher2 = (classObject2.has("teacher_name")) ? classObject2.getString("teacher_name") : "";
                String teacher3 = (classObject3.has("teacher_name")) ? classObject3.getString("teacher_name") : "";
                String teacher4 = (classObject4.has("teacher_name")) ? classObject4.getString("teacher_name") : "";
                String teacher5 = (classObject5.has("teacher_name")) ? classObject5.getString("teacher_name") : "";
                String teacher6 = (classObject6.has("teacher_name")) ? classObject6.getString("teacher_name") : "";

                showClassDetail(class1, class2, class3, class4, class5, class6, classroom1, classroom2, classroom3, classroom4, classroom5, classroom6, classTime1, classTime2, classTime3, classTime4, classTime5, classTime6, teacher1, teacher2, teacher3, teacher4, teacher5, teacher6);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showCards() {
        this.isDetail = false;
        this.mainActivity.sv_cards.setVisibility(View.VISIBLE);
        this.mainActivity.ll_detail.setVisibility(View.GONE);
        AnimationSet as = new AnimationSet(true);
        AlphaAnimation aa = new AlphaAnimation(0, 1);
        aa.setDuration(500);
        as.addAnimation(aa);
        this.mainActivity.sv_cards.startAnimation(as);
    }

    public void onTouch(MotionEvent event) {
        if (this.isDetail) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                this.showCards();
            }
        }
    }

    /**
     * 跳转到订阅界面
     */
    private void jumpToSubscribe() {
        Intent intent = new Intent(mainActivity, SubscriptionActivity.class);
        mainActivity.startActivityForResult(intent, 0);
    }

    /**
     * 通过model更新数据
     */
    private void reflashJSON() {
        refreshedData = DataModel.refresh();
        if (refreshedData != null) {
            newCardCount = refreshedData.length();
        }
    }

    /**
     * 读取完JSON之后刷新界面方法（可直接刷新）
     */
    public void reflashDone() {
        this.isReflashing = false;
        try {
            this.allData = new JSONArray(StorageUtils.retrieveFromFile("HistoryData.json"));
            if (refreshedData != null) {
                for (int i = 0; i < refreshedData.length(); i++) {
                    JSONObject cardData = (JSONObject) refreshedData.get(i);
                    allData.put(cardData);
                }
            }
            saveCardData();
            this.loadHistoryData();
            if (Constant.NEW_CARD_COUNT == 0) {
                if (newCardCount == 0) {
                    Toast.makeText(mainActivity.getBaseContext(), "没有新的卡片", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mainActivity.getBaseContext(), "有" + newCardCount + "张新的卡片", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(mainActivity.getBaseContext(), "有" + (Constant.NEW_CARD_COUNT + newCardCount) + "张新的卡片", Toast.LENGTH_SHORT).show();
            }
            Constant.NEW_CARD_COUNT = 0;
            newCardCount = 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void removeCardData(int index) {
        try {
            JSONArray newAllData = new JSONArray();
            for (int i = 0; i < this.allData.length(); i++) {
                if (i != index) {
                    newAllData.put(this.allData.get(i));
                }
            }
            allData = newAllData;
            saveCardData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showRemoveToast() {
        Toast.makeText(mainActivity.getBaseContext(), "卡片已删除", Toast.LENGTH_SHORT).show();
    }

    public boolean collectCardData(int index) {
        try {
            JSONArray collectedData = DataModel.retrieveCollectedData();
            if (collectedData == null) {
                collectedData = new JSONArray();
            }
            collectedData.put(this.allData.get(index));
            Toast.makeText(mainActivity.getBaseContext(), "卡片收藏成功", Toast.LENGTH_SHORT).show();
            return DataModel.saveCollectedData(collectedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public void saveCardData() {
        DataModel.saveHistoryData(allData);
    }

    /**
     * 同步数据
     */
    public void setting_sync() {
        mainActivity.showReflashDialog("同步数据中...");
        new Thread() {
            @Override
            public void run() {
                boolean ok = DataModel.sync();
                if (ok) {
                    handler.sendEmptyMessage(0x200);
                } else {
                    handler.sendEmptyMessage(0x401);
                }
            }
        }.start();
    }

    /**
     * 上课提醒时间
     */
    public void setting_lesson_alertTime(Integer time) {
        SharedPreferences sp = mainActivity.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt("classWarningTime", time);
        editor.commit();
    }

    /**
     * 考试提醒时间
     */
    public void setting_exam_alertTime(Integer time) {
        SharedPreferences sp = mainActivity.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt("examWarningTime", time);
        editor.commit();
    }

    /**
     * 检查更新
     */
    public void setting_checkUpdata() {
        UmengUpdateAgent.forceUpdate(mainActivity.getBaseContext());
        this.mainActivity.refresh = false;
    }

    /**
     * 意见反馈
     */
    public void setting_report() {
        FeedbackAgent agent = new FeedbackAgent(mainActivity);
        agent.startFeedbackActivity();
        this.mainActivity.refresh = false;
    }

    /**
     * 关于我们
     */
    public void setting_aboutUs() {
        mainActivity.ll_about.setVisibility(View.VISIBLE);
        Animation fadeIn = AnimationUtils.loadAnimation(mainActivity.getBaseContext(), R.anim.fade_in);
        mainActivity.ll_about.startAnimation(fadeIn);
    }


    /**
     * 注销
     */
    public void setting_logout() {
        DialogUtil.exitDialog(mainActivity, mainActivity, "确定要注销？");
    }

    /**
     * 展示详细天气卡片  － －不想写每个参数的注释－ －应该能看懂
     * 就是注意第一个参数是图片的drawableID
     *
     * @param city
     * @param top1
     * @param top2
     * @param temperature
     * @param small1
     * @param small2
     * @param small3
     * @param small4
     * @param yellow1
     * @param yellow2
     * @param yellow3
     * @param yellow4
     */
    private void showWeatherDetail(int weather_id, String city, String top1, String top2, String temperature, String small1, String small2, String small3,
                                   String small4, String yellow1, String yellow2, String yellow3, String yellow4) {
        this.mainActivity.ll_detail_three.setVisibility(View.GONE);
        this.mainActivity.ll_detail_weather.setVisibility(View.VISIBLE);
        this.mainActivity.ll_detail_class.setVisibility(View.GONE);
        this.mainActivity.iv_weather.setImageResource(weather_id);
        this.mainActivity.tv_weather_city.setText(city);
        this.mainActivity.tv_weather_top_1.setText(top1);
        this.mainActivity.tv_weather_top_2.setText(top2);
        this.mainActivity.tv_temperature.setText(temperature);
        this.mainActivity.tv_small_1.setText(small1);
        this.mainActivity.tv_small_2.setText(small2);
        this.mainActivity.tv_small_3.setText(small3);
        this.mainActivity.tv_small_4.setText(small4);
        this.mainActivity.tv_yellow_1.setText(yellow1);
        this.mainActivity.tv_yellow_2.setText(yellow2);
        this.mainActivity.tv_yellow_3.setText(yellow3);
        this.mainActivity.tv_yellow_4.setText(yellow4);
        this.showDetail();
    }

    /**
     * 显示那个只有三行信息的详细信息。。
     *
     * @param title
     * @param time
     * @param content
     */
    private void showThreeDetail(String title, String time, String content) {
        this.mainActivity.ll_detail_three.setVisibility(View.VISIBLE);
        this.mainActivity.ll_detail_weather.setVisibility(View.GONE);
        this.mainActivity.ll_detail_class.setVisibility(View.GONE);
        this.mainActivity.tv_detail_title.setText(title);
        this.mainActivity.tv_detail_time.setText(time);
        this.mainActivity.tv_detail_content.setText(content);
        this.showDetail();
    }

    /**
     * 显示课程表卡片的信息＝ ＝万恶的课程表
     *
     * @param class1
     * @param class2
     * @param class3
     * @param class4
     * @param class5
     * @param class6
     * @param classroom1
     * @param classroom2
     * @param classroom3
     * @param classroom4
     * @param classroom5
     * @param classroom6
     * @param classTime1
     * @param classTime2
     * @param classTime3
     * @param classTime4
     * @param classTime5
     * @param classTime6
     * @param teacher1
     * @param teacher2
     * @param teacher3
     * @param teacher4
     * @param teacher5
     * @param teacher6
     */
    private void showClassDetail(String class1, String class2, String class3, String class4, String class5, String class6,
                                 String classroom1, String classroom2, String classroom3, String classroom4, String classroom5, String classroom6,
                                 String classTime1, String classTime2, String classTime3, String classTime4, String classTime5, String classTime6,
                                 String teacher1, String teacher2, String teacher3, String teacher4, String teacher5, String teacher6) {
        this.mainActivity.ll_detail_class.setVisibility(View.VISIBLE);
        this.mainActivity.ll_detail_three.setVisibility(View.GONE);
        this.mainActivity.ll_detail_weather.setVisibility(View.GONE);
        this.mainActivity.tv_detail_class1.setText(class1);
        this.mainActivity.tv_detail_class2.setText(class2);
        this.mainActivity.tv_detail_class3.setText(class3);
        this.mainActivity.tv_detail_class4.setText(class4);
        this.mainActivity.tv_detail_class5.setText(class5);
        this.mainActivity.tv_detail_class6.setText(class6);
        this.mainActivity.tv_detail_teacher1.setText(teacher1);
        this.mainActivity.tv_detail_teacher2.setText(teacher2);
        this.mainActivity.tv_detail_teacher3.setText(teacher3);
        this.mainActivity.tv_detail_teacher4.setText(teacher4);
        this.mainActivity.tv_detail_teacher5.setText(teacher5);
        this.mainActivity.tv_detail_teacher6.setText(teacher6);
        this.mainActivity.tv_detail_classroom1.setText(classroom1);
        this.mainActivity.tv_detail_classroom2.setText(classroom2);
        this.mainActivity.tv_detail_classroom3.setText(classroom3);
        this.mainActivity.tv_detail_classroom4.setText(classroom4);
        this.mainActivity.tv_detail_classroom5.setText(classroom5);
        this.mainActivity.tv_detail_classroom6.setText(classroom6);
        this.mainActivity.tv_detail_classTime1.setText(classTime1);
        this.mainActivity.tv_detail_classTime2.setText(classTime2);
        this.mainActivity.tv_detail_classTime3.setText(classTime3);
        this.mainActivity.tv_detail_classTime4.setText(classTime4);
        this.mainActivity.tv_detail_classTime5.setText(classTime5);
        this.mainActivity.tv_detail_classTime6.setText(classTime6);
        this.showDetail();
    }

    public void testNotification() {
        if (!this.isNotificated) {
            this.sIntent = new Intent(this.mainActivity, MainService.class);
            this.mainActivity.startService(this.sIntent);
            this.isNotificated = true;
        }
    }

    public void stopNotification() {
        this.mainActivity.stopService(this.sIntent);
    }

    private void generateCard(JSONObject cardData) {
        try {
            String cardType = cardData.getString("card_type");
            if (cardType.equalsIgnoreCase("weather")) {
                WeatherCardBean weathercardbean = new WeatherCardBean();
                weathercardbean.setWeatherAddress(cardData.getString("Address"));
                weathercardbean.setWeatherConditions(cardData.getString("Condition"));
//              weathercardbean.setWeatherTemperatureDifference(cardData.getString("Temperature"));
                weathercardbean.setWeatherPM25(cardData.getString("pm2.5"));
                weathercardbean.setWeatherRainfall(cardData.getString("WindPower"));
                weathercardbean.setWeatherTemperature(cardData.getString("Temperature"));
                weathercardbean.setWeatherIcon(Integer.parseInt(cardData.getString("TodayIcon")));
                WeatherCardIni.generateWeatherCard(mainActivity.main_content_frame, R.layout.weather_card, mainActivity, mainActivity.getBaseContext(), weathercardbean);
                weathercardbean = null;
            } else if (cardType.equalsIgnoreCase("organization")) {
                NewsCardBean newscardbean = new NewsCardBean();
                newscardbean.setNewsTitle(cardData.getString("name"));
                newscardbean.setNewsActivity(cardData.getString("title"));
                newscardbean.setNewsAddress("地点：" + cardData.getString("place"));
                newscardbean.setNewsDate("时间：" + cardData.getString("start_time") + "~" + cardData.getString("end_time"));
                newscardbean.setNewsDeadline(cardData.getString("create_time"));
                newscardbean.setNewsIcon(R.drawable.love);
                NewsCardIni.generateNewsCard(mainActivity.main_content_frame, R.layout.news_card, mainActivity, mainActivity.getBaseContext(), newscardbean);
                newscardbean = null;
            } else if (cardType.equalsIgnoreCase("myclass")) {
                NewsCardBean newscardbean2 = new NewsCardBean();
                newscardbean2.setNewsTitle(cardData.getString("name"));
                newscardbean2.setNewsActivity(cardData.getString("title"));
                if (cardData.getString("content").length() > 20) {
                    newscardbean2.setNewsAddress("内容：" + cardData.getString("content").substring(0, 20) + "...");
                } else {
                    newscardbean2.setNewsAddress("内容：" + cardData.getString("content"));
                }
                newscardbean2.setNewsDeadline(cardData.getString("create_time"));
                newscardbean2.setNewsIcon(R.drawable.classmate);
                NewsCardIni.generateNewsCard(mainActivity.main_content_frame, R.layout.news_card, mainActivity, mainActivity.getBaseContext(), newscardbean2);
                newscardbean2 = null;
            } else if (cardType.equalsIgnoreCase("tutor")) {
                NewsCardBean newscardbean3 = new NewsCardBean();
                newscardbean3.setNewsTitle(cardData.getString("title"));
                newscardbean3.setNewsActivity(cardData.getString("name"));
                if (cardData.getString("content").length() > 20) {
                    newscardbean3.setNewsAddress("内容：" + cardData.getString("content").substring(0, 20) + "...");
                } else {
                    newscardbean3.setNewsAddress("内容：" + cardData.getString("content"));
                }
                newscardbean3.setNewsDeadline(cardData.getString("create_time"));
                newscardbean3.setNewsIcon(R.drawable.teacher);
                NewsCardIni.generateNewsCard(mainActivity.main_content_frame, R.layout.news_card, mainActivity, mainActivity.getBaseContext(), newscardbean3);
                newscardbean3 = null;
            }
            ///////////////////////////////////////////////////////////////////////////////////////////////
            //在这里
            else if((cardType.equalsIgnoreCase("official"))){
                NewsCardBean newscardbean4 = new NewsCardBean();
                newscardbean4.setNewsTitle(cardData.getString("name"));
                newscardbean4.setNewsActivity(cardData.getString("title"));
                if (cardData.getString("content").length() > 20) {
                    newscardbean4.setNewsAddress("内容：" + cardData.getString("content").substring(0, 20) + "...");
                } else {
                    newscardbean4.setNewsAddress("内容：" + cardData.getString("content"));
                }
                newscardbean4.setNewsDeadline(cardData.getString("create_time"));
                newscardbean4.setNewsIcon(R.drawable.logo);
                NewsCardIni.generateNewsCard(mainActivity.main_content_frame, R.layout.news_card, mainActivity, mainActivity.getBaseContext(), newscardbean4);
                newscardbean4 = null;
            } else if (cardType.equalsIgnoreCase("class")) {
                LessonCardBean lessoncardbean = new LessonCardBean();
                lessoncardbean.setLessonTitle(cardData.getString("class_name"));
                lessoncardbean.setLessonTeacher(cardData.getString("teacher_name"));
                lessoncardbean.setLessonTimeAndAddress(cardData.getString("class_room"));
                lessoncardbean.setLessonDay(cardData.getString("weekday") + " " + cardData.getString("class_order"));
                LessonCardIni.generateLessonCard(mainActivity.main_content_frame, R.layout.lesson_card, mainActivity, mainActivity.getBaseContext(), lessoncardbean);
                lessoncardbean = null;
            } else if (cardType.equalsIgnoreCase("ecard")) {
                ConsumptionCardBean consumptioncardbean = new ConsumptionCardBean();
                consumptioncardbean.setConsumptionTitle(cardData.getString("name"));
                consumptioncardbean.setConsumptionExpense("消费：" + cardData.getString("consume"));
                consumptioncardbean.setConsumptionAddress("地点：" + cardData.getString("place"));
                consumptioncardbean.setConsumptionDate("时间：" + cardData.getString("time"));
                consumptioncardbean.setConsumptionBalance("余额：" + cardData.getString("balance"));
                ConsumptionCardIni.generateConsumptionCard(mainActivity.main_content_frame, R.layout.consumption_card, mainActivity, mainActivity.getBaseContext(), consumptioncardbean);
                consumptioncardbean = null;
            }else if(cardType.equalsIgnoreCase("todo")){
                String Type = cardData.getString("type");
                TodoCardBean todoCardBean = new TodoCardBean();
                if (Type.equalsIgnoreCase("text")) {
                    todoCardBean.setTodoTitle(cardData.getString("due_time"));
                    todoCardBean.setTodoContent(cardData.getString("content"));
                    TodoCardIni.generateTodoCard(mainActivity.main_content_frame, R.layout.todo_card, mainActivity, mainActivity.getBaseContext(), todoCardBean);
                } else {
                    todoCardBean.setVoice_length("音频时长:"+cardData.getString("voice_length"));
                    todoCardBean.setTodoTitle(cardData.getString("due_time"));
                    todoCardBean.setVoice_path(cardData.getString("voice_path"));
                    TodoCardIni.generateTodoCard(mainActivity.main_content_frame, R.layout.todo_card, mainActivity, mainActivity.getBaseContext(), todoCardBean);
                }
                todoCardBean = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void cardsFlyIn(MainContentFrame main_content_frame) {
        int childCount = main_content_frame.getChildCount();
        if (childCount >= 3) {
            main_content_frame.getChildAt(0).setVisibility(View.VISIBLE);
            main_content_frame.getChildAt(1).setVisibility(View.VISIBLE);
            main_content_frame.getChildAt(2).setVisibility(View.VISIBLE);

            Animation animation1 = AnimationUtil.generateFlyInAnimation(750, "bottom");
            View test1 = main_content_frame.getChildAt(0);
            test1.startAnimation(animation1);

            Animation animation2 = AnimationUtil.generateFlyInAnimation(1000, "bottom");
            View test2 = main_content_frame.getChildAt(1);
            test2.startAnimation(animation2);

            Animation animation3 = AnimationUtil.generateFlyInAnimation(1250, "bottom");
            View test3 = main_content_frame.getChildAt(2);
            test3.startAnimation(animation3);
        }
        if (childCount == 2) {
            main_content_frame.getChildAt(0).setVisibility(View.VISIBLE);
            main_content_frame.getChildAt(1).setVisibility(View.VISIBLE);

            Animation animation1 = AnimationUtil.generateFlyInAnimation(750, "bottom");
            View test1 = main_content_frame.getChildAt(0);
            test1.startAnimation(animation1);

            Animation animation2 = AnimationUtil.generateFlyInAnimation(1000, "bottom");
            View test2 = main_content_frame.getChildAt(1);
            test2.startAnimation(animation2);
        }
        if (childCount == 1) {
            main_content_frame.getChildAt(0).setVisibility(View.VISIBLE);
            Animation animation1 = AnimationUtil.generateFlyInAnimation(750, "bottom");
            View test1 = main_content_frame.getChildAt(0);
            test1.startAnimation(animation1);
        }
    }

}
